package com.mengwen.backstage.config;

import com.mengwen.backstage.pojo.RankKeyName;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

/**
 * @author LiangYonghui
 * @date 2020/4/17 14:43
 * @description 定时任务
 */
@Configuration
@EnableScheduling
@Slf4j
public class SaticScheduleTask {

    @Autowired
    StringRedisTemplate stringRedisTemplate;



    /**
     * 每日十二点
     */
    @Scheduled(cron = "0 0 0 1/1 * ?")
    private void everydayTask() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        Boolean delete = stringRedisTemplate.delete(RankKeyName.ANSWER_DAY_KEY);
        if (!delete) {
            log.info("每天定期清空ANSWER失败");
        }
        Boolean delete1 = stringRedisTemplate.delete(RankKeyName.QUESTION_DAY_KEY);
        if (!delete1) {
            log.info("每天定期清空QUESTION失败");
        }
    }

    /**
     * 每周一
     */
    @Scheduled(cron = "0 0 0 ? * MON")
    private void everyweekTask() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        Boolean delete = stringRedisTemplate.delete(RankKeyName.ANSWER_WEEK_KEY);
        if (!delete) {
            log.info("每周定期清空ANSWER失败");
        }
        Boolean delete1 = stringRedisTemplate.delete(RankKeyName.QUESTION_WEEK_KEY);
        if (!delete1) {
            log.info("每周定期清空QUESTION失败");
        }
    }

    /**
     * 每月1号
     */
    @Scheduled(cron = "0 0 0 1 1/1 ?")
    private void everymonthTask() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        Boolean delete = stringRedisTemplate.delete(RankKeyName.ANSWER_MONTH_KEY);
        if (!delete) {
            log.info("每月定期清空ANSWER失败");
        }
        Boolean delete1 = stringRedisTemplate.delete(RankKeyName.QUESTION_MONTH_KEY);
        if (!delete1) {
            log.info("每月定期清空QUESTION失败");
        }
    }
}
